package jjn.carl.binary_tree;

import commons.TreeNode;

/**
 * @author Jiang Jining
 * @since 2023-07-18 23:32
 */
public class LeetCode530 {
    
    private TreeNode pre;
    private int minDifference;
    
    public int getMinimumDifference(TreeNode root) {
        this.minDifference = Integer.MAX_VALUE;
        traversal(root);
        return minDifference;
    }
    
    private void traversal(TreeNode current) {
        if (current == null) {
            return;
        }
        traversal(current.left);
        if (pre != null) {
            minDifference = Math.min(minDifference, current.val - pre.val);
        }
        pre = current;
        traversal(current.right);
    }
}
